package rabbitMQ.workQueue;

import com.rabbitmq.client.*;
import rabbitMQ.common.Common;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Common.HOST);
        factory.setPort(Common.PORT);
        factory.setVirtualHost(Common.VIRTUALHOST);
        factory.setUsername(Common.USERNAME);
        factory.setPassword(Common.PASSWORD);

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare(Common.WORK_QUEUE, true, false, false, null);
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收到消息: " + new String(body));
            }
        };
        channel.basicConsume(Common.WORK_QUEUE, true, consumer);
//        channel.close();
//        connection.close();
    }
}
